package com.cloudkinto.appletController.finance;


import com.cloudkinto.appletservice.finance.AppletRechargeRecordService;
import com.cloudkinto.base.BaseController;
import com.cloudkinto.common.annotations.UserSessionRequired;
import com.cloudkinto.common.utils.TimeUtils;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.service.finance.vo.RechargeAddReq;
import com.cloudkinto.service.finance.vo.RechargeRecordRes;
import com.cloudkinto.service.redpacket.WxRedPacketService;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ding
 * @since 2020-06-10
 */
@RestController
@Api(tags = "小程序财务")
@RequestMapping("/api/applet/recharge")
public class AppletRechargeRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(AppletRechargeRecordController.class);

    @Autowired
    private AppletRechargeRecordService service;
    @Autowired
    private WxRedPacketService redPacketService;

    @UserSessionRequired
    @GetMapping("/addInit")
    @ApiOperation(value = "新增初始化", notes = "新增初始化")
    public SingleResult addInit() {
        return service.addInit(getCompanyId());
    }

    @UserSessionRequired
    @PostMapping("/add")
    @ApiOperation(value = "新增", notes = "新增")
    public SingleResult add(@Validated @RequestBody RechargeAddReq req) {
        Long userId = getCurrentUserId();
        Long companyId = getCompanyId();
        return SingleResult.success("申请成功，请等待审核", service.add(req, companyId, userId));
    }

    @UserSessionRequired
    @GetMapping("/updateInit/{id}")
    @ApiOperation(value = "编辑初始化", notes = "编辑初始化")
    public SingleResult updateInit(@PathVariable("id") Long id) {
        return service.updateInit(id);
    }

    @UserSessionRequired
    @PutMapping("")
    @ApiOperation(value = "修改", notes = "修改")
    public SingleResult update(@RequestBody @Validated Object req) {
        Long userId = getCurrentUserId();
        return SingleResult.success("修改成功", service.update(req, userId));
    }

//    @UserSessionRequired
//    @DeleteMapping("/{id}")
//    @ApiOperation(value = "根据id删除", notes = "根据id删除")
//    public SingleResult delete(@PathVariable("id") Long id) {
//        Long userId = getCurrentUserId();
//        return SingleResult.success("删除成功", service.delete(id, userId));
//    }

    @UserSessionRequired
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查看详情", notes = "根据id查看详情")
    public SingleResult detail(@PathVariable("id") Long id) {
        return SingleResult.success("查询成功", service.detail(id));
    }

    @UserSessionRequired
    @GetMapping("/pageInit")
    @ApiOperation(value = "查询初始化", notes = "查询初始化")
    public SingleResult pageInit() {
        return service.pageInit();
    }

//    @UserSessionRequired
//    @GetMapping("/list")
//    @ApiOperation(value = "查询", notes = "查询")
//    public SingleResult list(String startDate, String endDate, String type) {
//        Long companyId = getCompanyId();
//
//        Map<String, String> map = new HashMap<>();
//        map.put("startDate", startDate);
//        map.put("endDate", endDate);
//        map.put("type", type);
//        map.put("companyId", companyId + "");
//        return SingleResult.success("查询成功", service.getList(map));
//    }

    @UserSessionRequired
    @GetMapping("/listPage")
    @ApiOperation(value = "查询（分页）", notes = "查询（分页）")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "startDate", value = "开始时间"),
            @ApiImplicitParam(name = "endDate", value = "结束时间"),
            @ApiImplicitParam(name = "type", value = "订单类型 空是全部 0 待审核  1充值成功 2充值取消")})
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = RechargeRecordRes.class)})//无效
    public PageResult<RechargeRecordRes> listPage(
            String startDate,
            String type,
            String current, String pageSize) {
        Long companyId = getCompanyId();
        Map<String, String> map = new HashMap<>();
        map.put("startDate", startDate);
        map.put("endDate", TimeUtils.getCurrentDateFormat(TimeUtils.parseDate(startDate), 1, 0));
        map.put("status", type);
        map.put("current", current);
        map.put("pageSize", pageSize);
        if (companyId != null)
            map.put("companyId", companyId + "");
        return service.getListPage(map);
    }


    @GetMapping("/wxPayRecharge")
    @ApiOperation(value = "微信支付申请", notes = "")
    public SingleResult wxPayRecharge(String companyId, Double money, String openId) {
        //1.根据code获取token以及openid
        return redPacketService.wxPayRecharge(companyId, money, openId);
    }

    @UserSessionRequired
    @GetMapping("/wxPayNativeRecharge")
    @ApiOperation(value = "微信支付申请", notes = "")
    public SingleResult wxPayNativeRecharge(Double money, String tips) {
        //1.根据code获取token以及openid
        return redPacketService.wxPayNativeRecharge(getCompanyId(), money, tips);
    }
    @UserSessionRequired
    @GetMapping("/aliPayRecharge")
    @ApiOperation(value = "支付宝支付", notes = "")
    public SingleResult aliPayRecharge(Double money) {
        //1.根据code获取token以及openid
        return redPacketService.aliPayRecharge(getCompanyId(), money);
    }

    @PostMapping("/wxPayNotify")
    @ApiOperation(value = "微信支付成功回调", notes = "")
    public Map wxPayNotify(@RequestHeader("Wechatpay-Signature") String authorization,
                           @RequestHeader("Wechatpay-Serial") String serialNo,
                           @RequestHeader("Wechatpay-Timestamp") String timestamp,
                           @RequestHeader("Wechatpay-Nonce") String nonce, @RequestBody Map<String, Object> wxPayNotifyData) {
        //1.根据code获取token以及openid
        System.out.println("==============支付回调===================");
        System.out.println("authorization:" + authorization);
        System.out.println("serialNo:" + serialNo);
        System.out.println("timestamp:" + timestamp);
        System.out.println("nonce:" + nonce);
        System.out.println("data:" + wxPayNotifyData);
        System.out.println("==============支付回调===================");
        return redPacketService.wxPayNotify(wxPayNotifyData, authorization, serialNo, timestamp, nonce);
//        return redPacketService.wxPayNotify(wxPayNotifyData, authorization, serialNo,timestamp,nonce);
    }

    @GetMapping("/wxPayRechargeQuery")
    @ApiOperation(value = "微信支付订单查询", notes = "")
    public SingleResult wxPayRechargeQuery(String companyId, String outTradeNo) {
        //1.根据code获取token以及openid

        return SingleResult.success(redPacketService.wxPayRechargeQuery(companyId, outTradeNo));
    }

    @UserSessionRequired
    @GetMapping("/wxPayNativeRechargeQuery")
    @ApiOperation(value = "微信支付订单查询", notes = "")
    public SingleResult wxPayRechargeQuery(String outTradeNo) {
        //1.根据code获取token以及openid

        return SingleResult.success(redPacketService.wxPayRechargeQuery(getCompanyId(), outTradeNo));
    }
}

